package com.pb.infra.auth.controller;

import com.pb.infra.auth.entity.pbsys.PriviCorp;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.auth.service.PriviCorpService;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.StringUtil;
import com.pb.infra.oth.util.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 法人管理
 * @author liudebao
 * @date 2020/1/1
 */
@Slf4j
@RestController
@RequestMapping("v1/system/corp")
public class PriviCorpController {

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "privi_corp";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "法人管理";

    /**
     * 业务逻辑对象
     */
    @Resource
    private PriviCorpService priviCorpService;

    /**
     * 读取法人信息
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @GetMapping("queryCorps")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result queryCorps(@RequestParam Map<String, Object> paramMap) {
        return priviCorpService.queryCorps(paramMap);
    }

    /**
     * 查询管理法人
     * @return Result 返回数据
     */
    @GetMapping("queryManageCorp")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result queryManageCorp() {
        return priviCorpService.queryManageCorp();
    }

    /**
     * 新建法人
     * @param priviCorp 法人实体类
     * @return Result 返回数据
     */
    @PostMapping("addCorp")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result addCorp(@RequestBody PriviCorp priviCorp, HttpServletRequest request) {
        Map<String, Object> map1 = priviCorpService.addCorp(priviCorp, request);
        if (map1 != null && !Constant.OK.equals(map1.get("code").toString()) && !StringUtil.isEmpty(map1.get("msg"))) {
            return Result.error(map1.get("code").toString(), map1.get("msg").toString());
        } else {
            return Result.ok(map1);
        }
    }

    /**
     * 修改法人
     * @param priviCorp 法人实体类
     * @return Result 返回数据
     */
    @PostMapping("editCorp")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result editCorp(@RequestBody PriviCorp priviCorp, HttpServletRequest request) {
        priviCorpService.editCorp(priviCorp, request);
        return Result.OK_EMPTY;
    }

    /**
     * 删除法人
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("removeCorp")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result removeCorp(@RequestBody List<String> paramMap) {
        if (priviCorpService.removeCorp(paramMap)) {
            return Result.OK_EMPTY;
        } else {
            return Result.error("-1", "该法人下面有未删除的用户");
        }
    }

    /**
     * 恢复法人
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("resumeCorp")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result resumeCorp(@RequestBody List<String> paramMap) {
        priviCorpService.resumeCorp(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 查询法人菜单信息
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @GetMapping("queryCorpMenus")
    public Result queryCorpMenus(@RequestParam Map<String, Object> paramMap) {
        return priviCorpService.queryCorpMenus(paramMap);
    }

    /**
     * 保存法人菜单信息
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("addCorpMenu")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result addCorpMenu(@RequestBody Map<String, Object> paramMap, HttpServletRequest request) {
        priviCorpService.addCorpMenu(paramMap, request);
        return Result.OK_EMPTY;
    }

}
